/**
 * 要加载下拉框的JS，直接调用JS封装好的方法（持续优化）;
 * 一般情况下只是CSS样式改变，采用默认的，因为都是动态获取的，不会重复
 */
/******************************************新开医嘱*******************************************************************************/
function adviceItemEvent(activeid){
	activeAdviceInputId = activeid;// 更新激活当前激活的input框ID
    var flagTop = $("#"+activeid).parent().position().top;
    var thiswidth,thisLeft,thisTop;
    if(flagTop >0){
    	 thiswidth=$("#"+activeid).parent().outerWidth();
    	 thisLeft=$("#"+activeid).parent().offset().left+30;
    	 thisTop=188;
    }else{
    	 thisLeft=$("#"+activeid).parent().offset().left;
    	 thisTop=$("#"+activeid).parent().offset().top;
    	 thisheight=$("#"+activeid).parent().outerHeight()+1;
    }

    var dorpcss = {
    		left:thisLeft+thiswidth,
			top:thisTop+"px",
			transform: "none",
			maxWidth:"1300px",
			display:"block"
    };
    var pass= {};
	pass.activeId = activeAdviceInputId;
	pass.item = 'aitem'; //医嘱项目
    
    var base = {};
	base.pass = pass;
	base.css = dorpcss
	
    dropSimpleBox(base);
}

/******************************************新开医嘱end*******************************************************************************/
/******************************************常用项目*******************************************************************************/
//常用项目编辑和删除
function editChangyongItem(that){
	debugger;
	event.stopPropagation(); 
	$(that).blur();
	var tr = $(that).parents("tr");
	$(tr).addClass("edit-status").siblings().removeClass("edit-status"); //呈现编辑状态
	var datastr = $(tr).attr("data-info");
	var dataobj = JSON.parse(datastr);
	var dorpcss = {
			top:"50%",
			left:"50%",
			maxWidth:"1300px",
			transform: "translate(-50%,-50%)",
			display:"block",
			width:"1200px"
    };
	var pass= {};
	pass.activeId = '';
	pass.item = 'citem'; //常用项目
	var base = {};
	base.pass = pass;
	base.css = dorpcss
	
	dropSimpleBox(base);	
}
//下拉框插件
function dropSimpleBox(base){
	closeSelfLayer();
	$("#dropbox").simpleDrop({
		menus : [{
			css:base.css,
			ids:{
				dropid:"tree_dorp_box_id",
				inputid:"drop_input_id",
				sbtnid:"drop_sbtn_id",
				leftid:"drop_left_id",
				tableid:"drop_table_id",
				pageid:"drop_page_id",
				spageid:"drop_spage_id",
			},
			which:"tpl1.html",
			event:function(ids){
				var pass = $.extend(base.pass, ids);
				operationEvent(pass);
			}
		}]
})
}
/******************************************常用项目end*******************************************************************************/
function operationEvent(pass){
	loadTreeList(pass);    //加载树
	keyUpSearch(pass); //搜索框回车事件
	dropBtnSearch(pass);  //按钮搜索
}
//加载下拉框的树
function loadTreeList(pass){ 
	var treearea = $("#"+pass.leftid); //此id是模板中默认的，可以自定义
	var lbeh01 = configBeh01();
	$.ajax({
		url: rootUrl+"/doctoradviceeditor/treelist",
		type: "get",
		data:{"lBEH01":lbeh01,"BCE01":bce01,"BCK01":bck01},
		dataType:'json',
		success:function(data)
		{
			var list = data.list;
			treearea.treeview({data: list});
			selectTreeNode(pass);
			
		}
	});
}
//回车搜索
function keyUpSearch(pass){
	$("#"+pass.inputid).keydown(function (e) {
      if (e.which === 13) {
    	  pass.sqltext = $("#"+pass.inputid).val();
    	  dropSearchByText(pass);
      }
      });
	$("#"+pass.sbtnid).on("click",function(){
		pass.sqltext = $("#"+pass.inputid).val();
		dropSearchByText(pass);
	})
}
// 根据首字母检索对应类型医嘱
function dropBtnSearch(pass)
{
	offset = 0;
	adviceSearch_public(pass);
}
function dropSearchByText(pass) 
{
	
	var reg= /^[A-Za-z]+$/; //判断是字母
	if (reg.test(pass.sqltext)){
		pass.sqltext = pass.sqltext.toLowerCase();
	}
	adviceSearch_public(pass);
}
//树节点选中事件
function selectTreeNode(pass){
	$('#'+pass.leftid).off('nodeSelected').on('nodeSelected', function(event, data) {
		$("#"+pass.inputid).val('');
		  // 查询详细项目列表
		  pass.bdo01 = data["bdo01"];
		  pass.curr = curr;
		  pass.bdm01 = data["bdm01"];
		  pass.bdm01 = data["bdm01"];
		  if(topTab=="Xiyao")
		  {
			  pass.lbda01 = "1";
			  // 查询西药品信息
			  medicineTableList(pass);
		  }
		  else if(topTab=="Zhongyao")
		  {
			  // 查询中药品信息
			  pass.lbda01 = "3";
			  medicineTableList(pass);
		  }
		  else
		  {
			  // 查询其它项目信息
			  otherTableList(pass);
		  }
		});
}
//加载tree-table内容(药品)
function medicineTableList(pass)
{
	debugger;
	var tabparam = activeTabParam();
	var bck01_smart = $("#"+tabparam.tabId).find(".zhongyao_zxks select").val();
	var lBDA01 = pass.lbda01,lBDO01 =pass.bdo01 ,inputId=pass.inputId,curr = pass.curr,bdm01=pass.bdm01;
	var bda01 = "";
	var bckyf = 0;
	switch(topTab)
	 {
	 	case "Xiyao" : bda01 = "('1','2')"; break;
	 	case "Zhongyao" : bda01 = "('3')"; break;
	 }
	$.ajax({
		url: rootUrl+"/doctoradviceeditor/medtreetodetail",
		type: "get",
		data:{"lBDO01":lBDO01,"lBDA01":lBDA01,"lACF01":1,"lSql":'%',"lBBX09":1,"bckyf":bck01_smart,"vaf58":0,"bda01":bda01,bdm01:bdm01,"yiyuanId":yiyuanId,
			"lBEH01":'3',"lBCE01":bce01,"lBCK01":bck01,"abc02":$("#abc02").text(),"bdp02":$("#patient_bdp02").text(),"BCKyf":bckyf,currPage:curr,pageSize:pageSize},
		dataType:'json',
		success:function(data)
		{
			data.bdm01 = bdm01;
			var list = data.list;
			$("#"+pass.pageid).show();
			$("#"+pass.pageid).empty();
			$("#"+pass.spageid).hide();
			var $xiangxixiangmu = $("#"+pass.tableid);
			if(list.length > 0){
				data.item= pass.item;
				var whicName = showNameByParamOrder();
				data.whicName= whicName;
				laytplUrl(myurl+"/module/tree-table-yaopin.html",data,function(data,html){
					$xiangxixiangmu.empty().append(html);
					$("#"+pass.dropid).find("table tbody tr:eq(0)").addClass("choseedTr");
					var pages=parseInt(data.total)%parseInt(pageSize);
					if(pages==0){
						totalPage=parseInt(data.total) / parseInt(pageSize);
					}else{
						totalPage=parseInt(data.total-pages) /parseInt(pageSize) + 1
					}
					laypage({
						cont: pass.pageid, 
						pages:totalPage, 
						groups: 5, //连续显示分页数
						curr: curr || 1, 
						jump: function(obj, first){ 
							if(!first){ 
								pass.curr = obj.curr;
								medicineTableList(pass);
							}
						}
					});
				});
			}else{
				$xiangxixiangmu.empty().append("<tr><td>暂无数据</td></tr>");
			}
		}
	});
}
//加载tree-table内容（非药品）
function otherTableList(pass)
{
	var lBDO01 =pass.bdo01 ,inputId=pass.inputId,curr = pass.curr;
	var bad01; 
	var beh01;
	switch(topTab)
	 {
	 	case "Jianyan" : bad01 = "L"; beh01="5"; break;
	 	case "Jiancha" : bad01 = "E"; beh01="4"; break;
	 	case "Shoushu" : bad01 = "S"; beh01="6"; break;
	 	case "Zhiliao" : bad01 = "T"; beh01="7"; break;
	 	case "Cailiao" : bad01 = "4"; beh01="8"; break;
	 }
	$.ajax({
		url: rootUrl+"/doctoradviceeditor/classificationretrieval",
		type: "post",
		data:{"lBDO01":lBDO01,"lACF01":1,"lSql":'%',"lBBX09":1,"lBEH01":beh01,"lBCE01":bce01,"lBCK01":bck01,"yiyuanId":yiyuanId,
			"abc02":"普通","bdp02":"自费","productId":9999,"programId":9999,"paramNo":60,"lbda01":bad01,currPage:curr,pageSize:pageSize},
		dataType:'json',
		success:function(data)
		{
			var list = data.list;
			$("#"+pass.pageid).show();
			$("#"+pass.pageid).empty();
			$("#"+pass.spageid).hide();
			var $xiangxixiangmu = $("#"+pass.tableid);
			if(list.length){
				data.item= pass.item;
				if(topTab=="Cailiao"){
					laytplUrl(myurl + "/module/tree-table-yaopin.html", data,function(data, html) {
						$xiangxixiangmu.empty().append(html);
						$("#"+pass.dropid).find("table tbody tr:eq(0)").addClass("choseedTr");
						var pages=parseInt(data.total)%parseInt(pageSize);
						if(pages==0){
							totalPage=parseInt(data.total) / parseInt(pageSize);
						}else{
							totalPage=parseInt(data.total-pages) /parseInt(pageSize) + 1
						}
						laypage({
							cont: pass.pageid, 
							pages:totalPage, 
							groups: 5, //连续显示分页数
							curr: curr || 1, 
							jump: function(obj, first){ 
								if(!first){ 
									pass.curr = obj.curr;
									otherTableList(pass);
								}
							}
						});
					});
				}
				else{
					laytplUrl(myurl+"/module/tree-table-feiyaopin.html",data,function(data,html){
						$xiangxixiangmu.empty().append(html);
						$("#"+pass.dropid).find("table tbody tr:eq(0)").addClass("choseedTr");
						var pages=parseInt(data.total)%parseInt(pageSize);
						if(pages==0){
							totalPage=parseInt(data.total) / parseInt(pageSize);
						}else{
							totalPage=parseInt(data.total-pages) /parseInt(pageSize) + 1
						}
						laypage({
							cont: pass.pageid, 
							pages:totalPage, 
							groups: 5, //连续显示分页数
							curr: curr || 1, 
							jump: function(obj, first){ 
								if(!first){ 
									pass.curr = obj.curr;
									otherTableList(pass);
								}
							}
						});
					});
					}
			}else{
				$xiangxixiangmu.empty().append("<tr><td>暂无数据</td></tr>");
			}
		}
	});
	
}
// 搜索医嘱调用
function adviceSearch_public(pass)
{
	var tabparam = activeTabParam();
	var bck01_smart = $("#"+tabparam.tabId).find(".zhongyao_zxks select").val();
	var sqltext = pass.sqltext;
	var sqltext = $("#"+pass.inputid).val();
	var reg= /^[A-Za-z]+$/; //判断是字母
	if (reg.test(sqltext)){
		sqltext = sqltext.toLowerCase();
	}
	$("#getBack_btn").attr("disabled",true);
	$("#getNext_btn").attr("disabled",true);
	var params = {};
	if (sqltext!='') {
		var beh01;
		switch (topTab) {
		case "Xiyao":
			beh01 = "1";
			break;
		case "Zhongyao":
			beh01 = "3";params.bckyf = bck01_smart;
			break;
		case "Jianyan":
			beh01 = "5";params.bckyf = 0;
			break;
		case "Jiancha":
			beh01 = "4";params.bckyf = 0;
			break;
		case "Shoushu":
			beh01 = "6";params.bckyf = 0;
			break;
		case "Zhiliao":
			beh01 = "7";params.bckyf = 0;
			break;
		case "Cailiao":
			beh01 = "8";params.bckyf = 0;
			break;
		}
		var url = rootUrl + "/clischemedefine/adviceSousuo";
		
		params.sqltext = '%' + sqltext + '%';
		params.acf01 = 1;
		params.bbx09 = 1;
		params.beh01 = beh01;
		params.bck01 = bck01;
		params.bce01 = bce01;
		params.abc02 = '普通';
		params.bdp02 = '自费';
		params.vaf58 = 0;
		params.yiyuanId = yiyuanId;
		params.offset = offset;
		$.get(url, params, function(data) {
			var $xiangxixiangmu = $("#"+pass.tableid);
			$("#"+pass.pageid).hide();
			$("#"+pass.spageid).show();
			if(data.list.length > 0){
				data.item= pass.item;
				if((topTab=="Xiyao")||(topTab=="Zhongyao")||topTab=="Cailiao"){
					var whicName = showNameByParamOrder();
					data.whicName= whicName;
					laytplUrl(myurl + "/module/tree-table-yaopin.html", data,function(data, html) {
						$xiangxixiangmu.empty().append(html);
						
//						$("#"+pass.dropid).find("table tbody tr:eq(0)").addClass("choseedTr");
					});
				}else{
					laytplUrl(myurl + "/module/tree-table-feiyaopin.html", data,function(data, html) {
						$xiangxixiangmu.empty().append(html);
//						$("#"+pass.dropid).find("table tbody tr:eq(0)").addClass("choseedTr");
					});
				}
			}else{
				$xiangxixiangmu.empty().append("<tr><td>无匹配数据</td></tr>");
			}
			debugger;
			//设置按钮状态
			setButtonStatus(data);
			//查询事件
			$("#"+pass.spageid).find("button").off("click").on("click",function(){
				var stype= $(this).attr("data-type");
				pass.stype = stype;
				adviceSearchPage(pass);
			})
		});
	}
}
function showNameByParamOrder(){
	var namep = valueArr["p_9999_9999_32"];
	var obj = {};
	obj.y_name = true;
	obj.t_name = true;
	switch (namep)
	{
		case '1' :
			obj.y_name = false;
			obj.t_name = true;
			break;
		case '2' :
			obj.y_name = true;
			obj.t_name = false;
			break;
		case '3' :
			break;
	}
	return obj;
}
//按钮状态
function setButtonStatus(data){
	$("#getBack_btn").attr("disabled",false);
	$("#getNext_btn").attr("disabled",false);
	//按钮状态处理
	//禁止上一步按钮
	if(offset==0)
	{
		$("#getBack_btn").attr("disabled",true);
		$("#getNext_btn").attr("disabled",false);
		if(!data || data.list.length<10)
		{
			$("#getBack_btn").attr("disabled",true);
			$("#getNext_btn").attr("disabled",true);
		}
	}
	else
	{
		$("#getBack_btn").attr("disabled",false);
		$("#getNext_btn").attr("disabled",false);
		if(!data || data.list.length<10)
		{
			$("#getBack_btn").attr("disabled",false);
			$("#getNext_btn").attr("disabled",true);
		}
	}
}
//查询分页事件
function adviceSearchPage(pass){
	var type = pass.stype;
	if(type==0)
	{
		offset = 0;
		adviceSearch_public(pass);
	}
	else if(type==-1)
	{
		offset -= 10;
		adviceSearch_public(pass);
	}
	else if(type==1)
	{
		offset += 10;
		adviceSearch_public(pass);
	}
}
//双击tree-table-tr的事件处理：药品
function addBbxInfoYaopin(str)
{
	var obj = $("#"+str).attr("data-info");
	obj=JSON.parse(obj);
	if(obj.item == "aitem"){
		addYaopin(obj,str);
	}else if(obj.item == "citem"){
		addChangyongItem(obj);
		$(".drop-container").hide();
	}
}
//双击tree-table-tr的事件处理：非药品
function addBbxInfoFeiyaopin(str)
{
	var obj = $("#"+str).attr("data-info");
	var obj=JSON.parse(obj);
	if(obj.item == "aitem"){
		feiyaopin(obj);
	}else if(obj.item == "citem"){
		addChangyongItem(obj);
		$(".drop-container").hide();
	}
}

